// Sprite by Andrew Davies

"use strict";

// http://www.html5canvastutorials.com/tutorials/html5-canvas-images/

function SpriteFrame( imgPosX, imgPosY, imgSizeX, imgSizeY )
{
	this.imgPos = new Vec2( imgPosX, imgPosY );
	this.imgSize = new Vec2( imgSizeX, imgSizeY );
}

function Sprite( imageSrc, animFramesPerGameFrame, gameFrameIdx )
{
	this.image = new Image();
	this.image.onload = function() 
	{
		//context.drawImage(imageObj, 69, 50);
    }
	this.image.src = imageSrc; // 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';
	
	this.anims = new Array();
	this.initialGameFrameIdx = gameFrameIdx;
	this.animFramesPerGameFrame = animFramesPerGameFrame;		
   
	this.update = function()
	{
		
	}
	
	this.draw = function( anim, posX, posY, sizeX, sizeY, alpha, gameFrameIdx )
	{
		var frameIdx = ( gameFrameIdx - this.initialGameFrameIdx ) * animFramesPerGameFrame;
		frameIdx = Math.floor( frameIdx );
		frameIdx %= anim.length;
		
		var frame = anim[ frameIdx ];
		
		context.globalAlpha = alpha;	
		context.drawImage( 
			this.image, 
			frame.imgPos.x, frame.imgPos.y,
			frame.imgSize.x, frame.imgSize.y,
			posX, posY,
			sizeX, sizeY );
		context.globalAlpha = 1.0;
	}
	
}
